﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Xejen.Communication.Socket.WebApi.Test.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpPost]
        public IActionResult Post([FromBody] WeatherForecastInputModel inputModel)
        {
            // 处理输入，可能保存到数据库等操作
            // 示例中直接返回输入的信息
            inputModel.TemperatureC += 3;
            inputModel.Summary += inputModel.Summary + "马志远";
            return Ok(inputModel);
        }

        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody] WeatherForecastInputModel inputModel)
        {
            // 处理输入，可能更新数据库中对应 id 的信息
            // 示例中直接返回输入的信息和传入的 id
            return Ok(new { Id = id, Input = inputModel });
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            // 处理删除操作，可能从数据库中删除对应 id 的信息
            // 示例中直接返回被删除的 id
            return Ok(new { DeletedId = id });
        }

        [HttpPost("add")]
        public IActionResult Add([FromBody] int value)
        {
            // 处理输入，可能进行加法操作等
            return Ok(value + 5);
        }

        [HttpGet("randomInt")]
        public IActionResult GetRandomInt()
        {
            var rng = new Random();
            return Ok("随机值是：" + rng.Next());
        }

        [HttpPost("addRange")]
        public IActionResult AddRange([FromBody] WeatherForecastInputModel[] inputModels)
        {
            // 处理输入，可能保存到数据库等操作
            // 示例中直接返回输入的信息数组
            return Ok(new { Nmae = "加了数组啦啦啦", Models = inputModels });
        }

        [HttpGet("randomForecastArray")]
        public IActionResult GetRandomForecastArray()
        {
            var rng = new Random();
            var forecastArray = Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            }).ToArray();

            return Ok(new { Nmae = "又来数组噢", Array = forecastArray });
        }
    }

    public class WeatherForecastInputModel
    {
        // 这里可以定义用于 POST 和 PUT 请求的输入模型
        public DateTime Date { get; set; }
        public int TemperatureC { get; set; }
        public string Summary { get; set; }
    }
}
